library(doBy);#  library(datrasR);   data(length.weight)
load(file=paste('/media/narwal/data.2013/1/Eflalo.15sep-30nov.2012.rda',sep="_")); head(eflalo.2012)
viris<-eflalo.2012[c("VE_REF","VE_ID","VE_FLT","VE_COU","VE_LEN","VE_KW","VE_TON","FT_REF","FT_DCOU","FT_DHAR","FT_DDAT","FT_DTIME",   
"FT_LCOU","FT_LHAR","FT_LDAT","FT_LTIME","LE_ID","LE_CDAT","LE_STIME","LE_ETIME","LE_SLAT","LE_SLON","LE_ELAT","LE_ELON",    
"LE_GEAR","LE_WIDTH","LE_MSZ","LE_RECT","LE_DIV","LE_SUBDIV","LE_MET","LE_KG_COD")] 
viris[is.na(viris$LE_KG_COD),]$LE_KG_COD<-0
head(viris)
viris$year <-as.numeric(substr(as.Date(viris$FT_DDAT,"%d/%m/%Y"),1,4))
viris$month <-as.numeric(substr(as.Date(viris$FT_DDAT,"%d/%m/%Y"),6,7))
viris$day <-as.numeric(substr(as.Date(viris$FT_DDAT,"%d/%m/%Y"),9,10))
viris$quar <-round(as.numeric(substr(as.Date(viris$FT_DDAT,"%d/%m/%Y"),6,7))/3+0.33,0) ;#round((inp.sh$month/3+0.33),0)
viris$DH<-0.0
viris$DH<-as.numeric(substr(viris$FT_DTIME,1,2))+as.numeric(substr(viris$FT_DTIME,4,5))/60
viris$AH<-0.0
viris$AH<-as.numeric(substr(viris$FT_LTIME,1,2))+as.numeric(substr(viris$FT_LTIME,4,5))/60
viris$DAS<-as.numeric(as.Date(viris$FT_LDAT,"%d/%m/%Y")-as.Date(viris$FT_DDAT,"%d/%m/%Y"))+(viris$AH-viris$DH)/24
viris<-viris[!is.na(viris$LE_MSZ),]
rm(kab.land)
kab.land<-aggregate(list(WEIGHT=viris$LE_KG_COD),
list(ship=viris$VE_REF,
year=viris$year,
quar=viris$quar,
LE_GEAR=viris$LE_GEAR,
LE_MSZ=viris$LE_MSZ,
DAS=viris$DAS,
power=viris$VE_KW,
DEP_DATE=viris$FT_DDAT,
DEP_TIME=viris$FT_DTIME),sum);dim(kab.land)
dim(unique(viris[c("VE_REF","year","quar","LE_GEAR","VE_KW","LE_MSZ","DAS","FT_DDAT","FT_DTIME")]));# unique(viris[c("year","quar","VE_REF","LE_GEAR","FT_DDAT","FT_DTIME","LE_MSZ")])
kab.land$KwDAS <- kab.land$DAS*kab.land$power
kab.land$metier<-NA
TRGR<-c("OTB","OTT","PTB","SDN","SSC","SPR")
kab.land[kab.land[]$LE_GEAR=="TBB"&kab.land[]$LE_MSZ>79&kab.land$LE_MSZ<120,]$metier<-"BT2"
kab.land[kab.land[]$LE_GEAR%in%TRGR&kab.land[]$LE_MSZ>79&kab.land$LE_MSZ<100,]$metier<-"TR2"
kab.land[kab.land[]$LE_GEAR%in%TRGR&kab.land[]$LE_MSZ>99&kab.land$LE_MSZ<120,]$metier<-"TR1C"
kab.land[kab.land[]$LE_GEAR%in%TRGR&kab.land[]$LE_MSZ>119,]$metier<-"TR1A"
kab.land<-kab.land[!is.na(kab.land$metier),]
sort<-unique(kab.land[c("year","quar","metier")])
orderBy(~year+quar+metier,data=sort)
kab.land<-kab.land[!(kab.land$year==2011&kab.land$quar<4),]
kab.land<-kab.land[!(kab.land$year==2012&kab.land$quar==4),]
unique(kab.land[c('year','quar')])
#for (year in 2008:2012) {
select.y<-list()
for (met in c("BT2","TR2","TR1C","TR1A")) {
sel.kab<-kab.land[kab.land$metier==met,]
if (length(sel.kab$year)>0) {
#sel.kab<-sel.kab[sel.kab$KwDAS>0,]
head(sel.kab)
sel<-list()
#sel$year<-year
sel$metier<-met
sel$land<-round(sum(sel.kab$WEIGHT),2)/12
sel$effo<-round(sum(sel.kab$KwDAS),2)/12
sel$LpUE<-round(sum(sel.kab$WEIGHT)/sum(sel.kab$KwDAS),3)
sel<-as.data.frame(sel)
select.y<-rbind(select.y,sel)
}
}
select.y
select<-list()
for (met in c("BT2","TR2","TR1C","TR1A")) {
for (quar in c(4,1,2,3) ) {
sel.kab<-kab.land[kab.land$quar==quar& kab.land$metier==met,]
if (length(sel.kab$year)>0) {
#sel.kab<-sel.kab[sel.kab$KwDAS>0,]
head(sel.kab)
sel<-list()
#sel$year<-year
sel$quar<-quar
sel$metier<-met
sel$land<-round(sum(sel.kab$WEIGHT),2)
sel$effo<-round(sum(sel.kab$KwDAS),2)
sel$LpUE<-round(sum(sel.kab$WEIGHT)/sum(sel.kab$KwDAS),3)
sel<-as.data.frame(sel)

select<-rbind(select,sel)
}
}
}
#}
unique(select$metier)
select.bt2<-select[select$metier=="BT2",]; colnames(select.bt2) <-c("quar","bt2.metier","bt2.land","bt2.effo","bt2.LpUE")
select.TR2<-select[select$metier=="TR2",]; 
select.TR1C<-select[select$metier=="TR1C",];
select.TR1A<-select[select$metier=="TR1A",];
result1<-cbind(merge(select.bt2,select.TR2,by="quar"),merge(select.bt2,select.TR1C,by="quar")[c("metier", "land","effo","LpUE")],merge(select.bt2,select.TR1A,by="quar")[c("metier","land","effo","LpUE")])
#setwd("d:/script/")
setwd("~/R-script/discards/")
write.csv(cbind(merge(select.bt2,select.TR2,by="quar"),merge(select.bt2,select.TR1C,by="quar")[c("metier","land","effo","LpUE")],
merge(select.bt2,select.TR1A,by="quar")[c("metier","land","effo","LpUE")]),file="res.csv")
dcf_dl_ratio_quar<-read.csv("http://ytgxgo.googlecode.com/svn/trunk/dcf_dl_ratio_quar_v20130115_v2.csv")
orderBy(~tuig+year+quar,data=dcf_dl_ratio_quar)

dcf_dl_ratio_quar<-dcf_dl_ratio_quar[dcf_dl_ratio_quar$tuig %in% c("BT2","TR2"),]
dcf_dl_ratio_quar<-orderBy(~tuig+year+quar,data=dcf_dl_ratio_quar)
colnames(dcf_dl_ratio_quar)
result.1<-cbind(result1,dcf_dl_ratio_quar[dcf_dl_ratio_quar$tuig %in% c("BT2"),][c(3,6,8)],dcf_dl_ratio_quar[dcf_dl_ratio_quar$tuig %in% c("TR2"),][c(3,6,8)])
colnames(result.1)<-c("quar","bt2.metier","bt2.land","bt2.effo",     
"bt2.LpUE","tr2.metier","tr2.land","tr2.effo",         
"tr2.LpUE","tr1c.metier","tr1c.land","tr1c.effo",         
"tr1c.LpUE","tr1a.metier","tr1a.land","tr1a.effo",         
"tr1a.LpUE","bt2.tuig","bt2.mean_dl_ratio","bt2.SE_dl_ratio",  
"tr2.tuig","tr2.mean_dl_ratio","tr2.SE_dl_ratio")  

BT2.CpUE<-sum(result.1$bt2.mean_dl_ratio*result.1$bt2.land/100+result.1$bt2.land)/sum(result.1$bt2.effo)
TR2.CpUE<-sum(result.1$tr2.mean_dl_ratio*result.1$tr2.land/100+result.1$tr2.land)/sum(result.1$tr2.effo)
TR2.CpUE/BT2.CpUE
ratio1<-rnorm(1000,result.1$bt2.mean_dl_ratio[1],result.1$bt2.SE_dl_ratio)

